<?php
namespace Admin\Controller;
class ProductController extends BaseController{
	public $pagesize=10;
	public $currentpage=1;
	public $where='ischeck=0';
	function productAdd(){	
		
		
		$result = $this->auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $_COOKIE['uid']);
		
		/* if(!$result){
		
			exit();
		} */			
		if(!empty($_GET)){//判断客户端是否有选择产品分类
			setcookie('typeid',$_GET['typeid']);
			setcookie('typefid',$_GET['typefid']);		
		}
		
		if(!empty($_POST)){ //判断是否进行数据添加
			if(isset($_POST['action'])){//提交按钮点击后，先检查分类是否选择
				if($_POST['action']=='checking'){
					if(isset($_COOKIE['typeid'])){
						$info =array('mes'=>4,);
						$this->ajaxReturn($info);
						
					}else{
						$info =array('mes'=>5,'content'=>'请选择左侧《产品类型列表》中的类型！');
						$this->ajaxReturn($info);
						exit();
					}
				}
			};
			//处理数据
			$_POST['typeid']= $_COOKIE['typeid'];
			$_POST['typefid']= $_COOKIE['typefid'];
			unset($_COOKIE); //消除cookie

			$pdoProduct = D('Product');
			$ce = $pdoProduct->create();
			if($ce){
				//上传数据
				$re=$pdoProduct->add();
		
				if($re){
					$id = $pdoProduct->getLastInsID();	//获取插入后的ID
					//图片文件处理并得到一张缩略图
					//上传图片
					$pdoUpload = new \Think\Upload();
					$pdoUpload->mimes=array('image/jpeg','image/png','image/gif');
					$pdoUpload->autoSub=false;
					$pdoUpload->rootPath= "./Public/upload/uploads/";
					$imageArr = $pdoUpload->upload();
					
					if($imageArr){
						//生成缩略图，并获得图片名
						foreach ($imageArr as $v){
							$imageName = $v['savename'];
							$pdoThumbImage =new \Think\Image();
							$pdoThumbImage->open("./Public/upload/uploads/".$imageName)
							->thumb(100,100,\Think\Image::IMAGE_THUMB_FILLED)
							->save("./Public/images/".$imageName);
							//保存到数据
							$data=array(
									'productid'=>$id,
									'imagename'=>$imageName,
									'time'=>time()
							);
							$pdoAddImage = M('Productimage');
							$reAddImage = $pdoAddImage ->data($data)->add();
							if($reAddImage){
								unset($_POST);
								$mes=1;	
								$this->assign('mes',$mes);		
		
							}else{
								$mes=2;	
								$this->assign('mes',$mes);
							}
						}
		
					}else{
						$mes=3;	
						$this->assign('mes',$mes);
					}
		
				}
		
			}else{
				$error = $pdoProduct->getError();

				$this->assign('post',$_POST);
				$this->assign('error',$error);
				$mes=6;
				$this->assign('mes',$mes);

			}
			
		}else{
			//查询风格样式
			$pdoStyle = new \Think\Model('style');
			$arrStyle = $pdoStyle->field(array('id','stylename'))->where(array('status'=>':status'))->bind(':status',1)->select();
			//查询材质
			$pdoMaterail = new \Think\Model('materail');
				//防注入
			$arrMaterail = $pdoMaterail->field(array('id','materailname'))->where(array('status'=>':status'))->bind(':status',1)->select();
			//传值到模板
			$this->assign('arrStyle',$arrStyle);
			$this->assign('arrMaterail',$arrMaterail);
		}
		$this->display();
		
	}
	
	
	//商品管理页面
	function productOper(){
		//$this->display();
	}
	function productC(){
	
		
		$pdo = new \Think\Model('product');//D('Product');
		//判断是否有指定查询分类的参数
		if(!empty($_POST)){
			//有指定查询参数进行指定查询
			if(isset($_POST['pagesize'])){//判断客户端是否有指定每页显示的记录条数
				$this->pagesize =$_POST['pagesize'];
			}
			if(isset($_POST['currentpage'])){//判断客户端是否有页面跳转操作
				$this->currentpage =$_POST['currentpage'];
			}
			if(isset($_POST['where'])){//判断客户端是否有选择性的查询操作
				$this->where ='ischeck=0 and '.$_POST['where'];
			}
			$start=($this->currentpage-1)*$this->pagesize; //起始值
	
		}else{
	
			//无指定查询进行全表查询
	
			//获取总记录条数
			$count = $pdo->where("ischeck=0")->count();
			$start=($this->currentpage-1)*$this->pagesize;//获取每页显示的起始值
		};
		$arrProduct = $pdo->where('ischeck=0')->limit("$start,$this->pagesize")->select();
	
		$this->assign('arrProduct',$arrProduct);
		$this->display();
	}	
	
	//列表
	function productList(){
		$pdo = D('product');
		//获得总的记录条数，计算出总的页面
		$count = $pdo->count();
		$this->assign('count',$count);
		//获取分页显示的起始值
		if(empty($_GET)){
			
			$start = ($this->currentpage-1)*$this->pagesize;
			$pdoArr = $pdo->limit("$start,$this->pagesize")->select();
			foreach ($pdoArr as $k=>$v){//查询风格样式
				$pdoStyle = D('Style');
				$styleArr = $pdoStyle->field('stylename')->where(array('id'=>$v['style']))->select();
				$pdoArr[$k]['style'] = $styleArr[0]['stylename'];
				//查询材质
				$pdoMaterail = D('Materail');
				$materailArr = $pdoMaterail->field('materailname')->where(array('id'=>$v['material']))->select();
				$pdoArr[$k]['material'] = $styleArr[0]['materailname'];
			
			};
		
		}else{
			if(isset($_GET['currentpage'])){
				$this->currentpage = $_GET['currentpage'];
			};
			if(isset($_GET['pagesize'])){
				$this->pagesize = $_GET['pagesize'];
			};
			
			$start = ($this->currentpage-1)*$this->pagesize;
			$pdoArr = $pdo->limit("$start,$this->pagesize")->select();
			foreach ($pdoArr as $k=>$v){//查询风格样式
				$pdoStyle = D('Style');
				$styleArr = $pdoStyle->field('stylename')->where(array('id'=>$v['style']))->select();
				$pdoArr[$k]['style'] = $styleArr[0]['stylename'];
				//查询材质
				$pdoMaterail = D('Materail');
				$materailArr = $pdoMaterail->field('materailname')->where(array('id'=>$v['material']))->select();
				$pdoArr[$k]['material'] = $styleArr[0]['materailname'];
			
			};
		}
		$this->assign('productArr',$pdoArr); 
		$this->display();
	}
	
}

